package com.rent.product.service.impl;

import com.rent.product.entity.PmsCategoryEntity;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rent.product.dao.PmsCategoryDao;
import com.rent.product.service.PmsCategoryService;


@Service("pmsCategoryService")

//已加入泛型Dao的实现 PmsCategoryDao = BaseMapper ,pmsCategoryDao = baseMapper
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryDao, PmsCategoryEntity> implements PmsCategoryService {


    @Override
    public List<PmsCategoryEntity> listWithTree() {
        //1.查出所有分类
        List<PmsCategoryEntity> entities = baseMapper.selectList(null);

        // List<PmsCategoryEntity> entities = pmsCategoryDao.listWithTree();

        //2.组装父子类的树型结构
        //2.1 找到所有的一级分类
        /*使用filter过滤一级分类(getParentCid() == 0),将一级分类收集成一个集合,
          通过map方式，将当前一级分类的子分类，重新返回给一级分类，再收集成list
          sorted排序*/
        List<PmsCategoryEntity> level1Menus = entities.stream().filter(pmsCategoryEntity ->
                pmsCategoryEntity.getParentCid() == 0
        ).map((menu)->{
            menu.setChildren(getChildren(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return level1Menus;
    }

    @Override
    public void removeMenuByIds(List<String> asList) {
        baseMapper.removeMenuByIds(asList);
    }

    @Override
    public List<PmsCategoryEntity> findLastId() {
        List<PmsCategoryEntity> pmsCategoryList = baseMapper.findLastId();

        return pmsCategoryList;
    }

    @Override
    public void save01(PmsCategoryEntity pmsCategory) {
        baseMapper.save01(pmsCategory);
    }

    @Override
    public List<PmsCategoryEntity> listAll() {
        return baseMapper.listAll();
    }

    @Override
    public int deleteById(String cat_id) {
        return baseMapper.deleteById(cat_id);
    }

    @Override
    public List<PmsCategoryEntity> findPidById(String cat_id) {
        List<PmsCategoryEntity> pmsCategoryEntities=baseMapper.findPidById(cat_id);
        return pmsCategoryEntities;
    }


    /*
     * 通过数组查找到当前一级分类的子菜单，root为当前一级分类,all为所有一级分类
     * 递归查找所有的子菜单,排序后返回收集
     * */
    private List<PmsCategoryEntity> getChildren(PmsCategoryEntity root, List<PmsCategoryEntity> all){

        List<PmsCategoryEntity> children = all.stream().filter(pmsCategoryEntity -> {
            return pmsCategoryEntity.getParentCid() == root.getCatId();
        }).map(pmsCategoryEntity -> {
            //(递归)找到子菜单
            pmsCategoryEntity.setChildren(getChildren(pmsCategoryEntity,all));
            return pmsCategoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public String getCompleteCategory(Long catalogId) {
        String string = "";
        while(true){
            PmsCategoryEntity pmsCategoryEntity = this.getById(catalogId);
            if(pmsCategoryEntity != null){
                catalogId = pmsCategoryEntity.getParentCid();
                string = pmsCategoryEntity.getName() + "/" + string;
                if(catalogId == 0)
                    break;
            }else
                break;
        }
        return string.substring(0,string.length() - 1)  ;
    }

    @Override
    public List<HashMap<String, Object>> getClassificationLists() {
        List<HashMap<String, Object>> classificationlists = new ArrayList<>();
        List<PmsCategoryEntity> pmsCategoryEntityList = this.list();
        for(int i = 0; i < pmsCategoryEntityList.size(); i++){
            PmsCategoryEntity pmsCategoryEntity = pmsCategoryEntityList.get(i);
            Long parentCid = pmsCategoryEntity.getParentCid();
            for(int j = 0; j < classificationlists.size(); j++){

            }
        }


        return classificationlists;
    }
}